﻿import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import FilUI

import "../controls"

Item {
    id: control

    property string currentPath: "/"
    property string requestPath: "/"
    property var reqItems: []
    property var okItems: []
    property bool progressRing: false
    property int countdown: 0
    property alias countdownText: id_filtext__countdown.text

    // public controls
    property alias filIconButton__refresh: id_filiconbutton__refresh
    property alias filIconButton__return: id_filiconbutton__return
    property alias filBreadcrumbBar__path: filbreadcrumbar__path
    property alias filText__countdown: id_filtext__countdown

    signal getPathBarPathList()

    onReqItemsChanged: {
        console.log("onReqItemsChanged=", JSON.stringify(reqItems))
    }

    QtObject {
        id: d
        property bool isOnClickItemSignal: false
        property var model
        property int removeModelIndex: 0
    }

    state: "idle"
    states: [
        State {
            name: "idle"
            PropertyChanges {
                id_filiconbutton__refresh.disable: false
                id_filiconbutton__return.disable: false
                filbreadcrumbar__path.disable: false
                control.progressRing: false
            }
        },
        State {
            name: "busy"
            PropertyChanges {
                id_filiconbutton__refresh.disable: true
                id_filiconbutton__return.disable: true
                filbreadcrumbar__path.disable: true
                control.progressRing: true
            }
        }
    ]

    height: 30
    width: 300
    RowLayout {
        anchors.fill: parent
        spacing: 0

        FilIconButton {
            id: id_filiconbutton__refresh
            height: control.height
            text: qsTr("Refresh")
            iconSource: FilIcons.Refresh
            iconSize: 14
            color: "transparent"
            verticalPadding: 0
            horizontalPadding: 0
            iconColor: {
                if (!enabled) {
                    return Qt.rgba(161/255, 161/255, 161/255, 1)
                }
                if (pressed) {
                    return Qt.rgba(0, 0, 0, 0.6)
                }
                return hovered ? Qt.rgba(0, 0, 0, 0.3) : Qt.rgba(0, 0, 0, 1)
            }
            onClicked: {
                var items = filbreadcrumbar__path.items
                control.reqItems = items
                getPathBarPathList()
            }
        }

        FilIconButton {
            id: id_filiconbutton__return
            height: control.height
            text: qsTr("Return Parent Directory")
            iconSource: FilIcons.RevToggleKey
            iconSize: 14
            color: "transparent"
            verticalPadding: 0
            horizontalPadding: 0
            iconColor: {
                if (!enabled) {
                    return Qt.rgba(161/255, 161/255, 161/255, 1)
                }
                if (pressed) {
                    return Qt.rgba(0, 0, 0, 0.6)
                }
                return hovered ? Qt.rgba(0, 0, 0, 0.3) : Qt.rgba(0, 0, 0, 1)
            }
            onClicked: {
                var items = filbreadcrumbar__path.items
                var count = filbreadcrumbar__path.count()
                if (count > 1) {
                    items.pop()
                    control.reqItems = items
                    getPathBarPathList()
                }
            }
            // debug test
            // onDoubleClicked: {
            //     var items = filbreadcrumbar__path.items
            //     for (var i = 0; i < 10; i++) {
            //         items.push({title: qsTr("Item_") + (i + 1)})
            //     }
            //     filbreadcrumbar__path.items = items
            // }
        }

        FilBreadcrumbBar {
            id: filbreadcrumbar__path
            Layout.fillWidth: true
            Layout.leftMargin: 7
            separator: ">"
            spacing: 8
            textSize: 18
            onClickItem: (model) => {
                console.log("onClickItem index:%1 count()=%2".arg(model.index).arg(count()))
                if (model.index + 1 !== count()) {
                    d.isOnClickItemSignal = true
                    d.model = model
                    d.removeModelIndex = model.index
                    // request new info
                    var index = model.index + 1
                    var items = filbreadcrumbar__path.items
                    control.reqItems = items.slice(0, index)
                    getPathBarPathList()
                }
            }
            Component.onCompleted: {
                var items = []
                items.push({title: "/" })
                control.reqItems = items
                control.okItems = items
                getPathBarPathList()
            }
            onItemsChanged: {
                console.log("onItemsChanged=", JSON.stringify(items))
            }
        }

        Item {
            width: 30
            height: 30
            Layout.alignment: Qt.AlignVCenter
            visible: control.progressRing
            FilProgressRing {
                width: 20
                height: 20
                anchors.centerIn: parent
                strokeWidth: 3
            }
        }

        Item {
            width: 30
            height: 30
            Layout.alignment: Qt.AlignVCenter
            visible: !control.progressRing
            FilText {
                id: id_filtext__countdown
                width: 20
                height: 20
                anchors.centerIn: parent
                text: ""
            }
        }

    }

    Timer {
        id: id_timer__countdown
        interval: 1000
        repeat: true
        onTriggered: {
            control.countdown--
            control.countdownText = "%1".arg(control.countdown)
            if (control.countdown == 0) {
                this.stop()
                getPathBarPathList()
            }
        }
    }

    function restartCountDown() {
        control.countdown = SettingsHelper.getHeartbeatInterval()
        control.countdownText = "%1".arg(control.countdown)
        console.log("restartCountDown %1s".arg(control.countdown))
        if (control.countdown <= 0) {
            console.log("restartCountDown cancel auto refresh")
            id_timer__countdown.stop()
        } else {
            id_timer__countdown.restart()
        }
    }

    function stopCountDown() {
        console.log("stopCountDown")
        id_timer__countdown.stop()
    }

    function getRequestPath() {
        var items = control.reqItems
        var finalPath = ""
        items.forEach((item, index) => {
            finalPath += item.title
            if (index !== 0 && index !== items.length - 1)
                finalPath += "/"
        })
        if (finalPath.length <= 0) {
            console.debug("invalid finalPath length")
            return "/"
        }
        control.requestPath = finalPath
        return control.requestPath
    }
    function entryFolder(folder) {
        if (typeof folder !== "string") {
            console.warn("entryFolder Invalid folder string format")
            return
        }
        if (control.state === "busy") {
            console.warn("entryFolder busy status, not do it.")
            return
        }
        var itemss = filbreadcrumbar__path.items
        var items = []
        itemss.forEach((item, index) => {
            items.push(item)
        })
        items.push({title: folder})
        control.reqItems = items
        getPathBarPathList()
    }
    function errorFolder() {
        console.log("errorFolder")
        // var items = control.okItems
        // filbreadcrumbar__path.items = items
    }
    function okFolder() {
        console.log("okFolder d.isOnClickItemSignal=%1".arg(d.isOnClickItemSignal))
        console.log("okFolder control.reqItems=", JSON.stringify(control.reqItems))
        var items = control.reqItems
        control.okItems = items
        filbreadcrumbar__path.items = items
        // if (d.isOnClickItemSignal) {
        //     d.isOnClickItemSignal = false
        //     filbreadcrumbar__path.remove(d.removeModelIndex + 1, filbreadcrumbar__path.count() - d.removeModelIndex - 1)
        // }
    }
}
